<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Project.
 *
 * @package namespace App\Repository\Models;
 */
class Project extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;
    protected $table = 'projects';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['uid','admin_id','type','title','city_id','project_owner','content','contacts','phone','purchase_type','purchase_mode','check_status','check_status_reason','created_at','updated_at','announcement_type','project_type'];

    protected $appends = [
        'project_id',
        'announcement_type_name',
        'purchase_type_name',
        'purchase_mode_name',
        'type_name',
        'check_status_name'
    ];

    protected $hidden = ['id'];

    //审核状态
    const CHECK_STATUS_UNCHECK = 'uncheck';
    const CHECK_STATUS_PASS = 'pass';
    const CHECK_STATUS_FAILED = 'failed';

    //审核情况
    public static $check_status_map = [
        'uncheck' => '未审核',
        'pass' => '已通过',
        'failed' => '审核失败'
    ];

    //项目发布类型
    public static $type_map = [
        'official' => '官方',
        'member' => '会员',
        'certified' => '认证会员',
    ];

    //公告类型，0作为‘其它’使用
    public static $announcement_type_map = [
        1 => '采购预报',
        2 => '采购公告',
        3 => '中标成交公示',
        4 => '采购合同公告',
        5 => '竞争性磋商公告',
        6 => '单一来源公示',
        7 => '进口产品公示',
        8 => '采购文件或需求公示',
        9 => '公示'
    ];

    //采购方式，0作为‘其它’使用
    public static $purchase_mode_map = [
        1 => '公开招标',
        2 => '邀请招标',
        3 => '竞争性谈判',
        4 => '询价采购',
        5 => '单一来源采购',
        6 => '竞争性磋商'
    ];

    //采购类型，0作为‘其它’使用
    public static $purchase_type_map = [
        1 => '政府集中采购',
        2 => '部门集中采购',
        3 => '分散采购委托代理',
        4 => '分散采购自行组织',
        5 => '单位自行采购'
    ];

    //项目类型，0作为‘其它’使用
    public static $project_type_map = [
        1 => '建筑设备自动化系统',
        2 => '多媒体及会议系统',
        3 => '安防系统',
        4 => '综合布线系统',
        5 => '通讯网络系统',
        6 => '智能楼宇系统',
        7 => '智能交通系统',
        8 => '智能家居系统'
    ];

    public static $project_time_map = [
        'three_day' => '近3天',
        'week' => '近7天',
        'half_month' => '近半月',
        'month' => '近一月'
    ];


    public function getCheckStatusNameAttribute()
    {
        return self::$check_status_map[$this->attributes['check_status']];
    }
    public function getProjectIdAttribute()
    {
        return $this->attributes['id'];
    }

    public function setProjectIdAttribute($val)
    {
        $this->attributes['id'] = $val;
    }
    public function getTypeNameAttribute()
    {
        return self::$type_map[$this->type];
    }

    public function getAnnouncementTypeNameAttribute()
    {
        $announcement_type_id = $this->attributes['announcement_type'];
        return $announcement_type_id ? self::$announcement_type_map[$announcement_type_id] : '其它';
    }

    public function getPurchaseModeNameAttribute()
    {
        $purchase_mode_id = $this->attributes['purchase_mode'];
        return $purchase_mode_id ? self::$purchase_mode_map[$purchase_mode_id] : '其它';
    }

    public function getPurchaseTypeNameAttribute()
    {
        $purchase_type_id = $this->attributes['purchase_type'];
        return $purchase_type_id ? self::$purchase_type_map[$purchase_type_id] : '其它';
    }

    public function region()
    {
        return $this->belongsTo(Regions::class, 'city_id', 'id');
    }
    public function admin_user()
    {
        return $this->hasOne(AdminUser::class,'id','admin_id');
    }
}
